import sys, getopt, os

root = '../data/'
help = 'run.py -s 预处理 -a 分析 -p 生成训练集'

def main(args):
    try:
        opts, args = getopt.getopt(args, 'hs:', ['save='])
    except getopt.GetoptError:
        print(help)
        sys.exit(2)
    for opt, arg in opts:
        if opt == '-h':
            print(help)
            sys.exit()
        elif opt in ('-s', '--save'):
            run_save(arg)

# 数据预处理
def run_save(arg):
    url = root + arg
    cmd = 'python Data_reset.py '
    folder_output(cmd=cmd, url=url, filter='_out.csv')

def folder_output(cmd, url, filter, isin=False):
    count = 0
    if '.csv' in url:
        run(cmd + url)
    else:
        list = os.listdir(url)  # 列出文件夹下所有的目录与文件
        for i in range(0, len(list)):
            path = os.path.join(url, list[i])
            if os.path.isfile(path):
                if isin:
                    if (filter in path):
                        count = count + 1
                        print('正在处理第', count, '个文件')
                        run(cmd + path)
                else:
                    if (filter not in path):
                        count = count + 1
                        print('正在处理第', count, '个文件')
                        run(cmd + path)

def run(cmd):
    print(cmd)
    os.system(cmd)


if __name__ == '__main__':
    main(sys.argv[1:])
